﻿using System.ComponentModel.DataAnnotations.Schema;
using Core.Domain.Enums;

namespace Core.Domain.Entities;

#nullable disable

/// <summary>
/// 用户组织表（多对多关联表 + 职位信息）
/// </summary>
[Table("UserOrganizations")]
public class UserOrganization : AuditableEntity
{
    /* 1.1 数据：全部私有 set */
    public Guid UserId { get; private set; }
    public Guid OrganizationId { get; private set; }
    public OrganizationPosition Position { get; private set; } // 职位

    public virtual User User { get; private set; }
    public virtual Organization Organization { get; private set; }

    /* 1.2 构造保护不变式 */
    private UserOrganization() { } // EF Core 使用

    public UserOrganization(User user, Organization organization, OrganizationPosition position = OrganizationPosition.Member)
    {
        UserId = user.Id;
        OrganizationId = organization.Id;
        Position = position;
    }

    /* 业务行为：调整职位 */
    public void SetPosition(OrganizationPosition newPosition)
    {
        if (Position == newPosition) return;
        Position = newPosition;
        // 可选：发布领域事件
        // QueueDomainEvent(new UserPositionChangedInOrganization(UserId, OrganizationId, newPosition));
    }
}
